package admin.dao;

import admin.entity.Adenty;
import admin.utils.DBConn;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Addao {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    // 显示管理员信息
    public ArrayList<Adenty> getAdInfo() {
        DBConn obj = new DBConn();
        Connection conn = obj.getConn();
        ArrayList<Adenty> adenties = new ArrayList<>();

        if (conn == null) {
            System.out.println("连接出错");
            return null;
        }
        String sql = "select * from administrator";
        try {
            PreparedStatement pstm = conn.prepareStatement(sql);
            ResultSet resultSet = pstm.executeQuery();
            while (resultSet.next()) {
                Adenty adenty = new Adenty();
                adenty.setCid(resultSet.getString("cid"));
                adenty.setCpwd(resultSet.getString("cpwd"));
                adenty.setCname(resultSet.getString("cname"));
                adenty.setCsex(resultSet.getString("csex"));
                adenty.setCcall(resultSet.getString("ccall"));
                adenties.add(adenty);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println("sql语句错了");
            return null;
        }
        return adenties;
    }

    //管理员登录
    public Adenty login(Connection conn, Adenty adenty) throws Exception {
        Adenty resultAd = null;
        // sql 查询语句
        String sql = "select * from administrator where cid=? and cpwd=?";
        // 获得执行sql语句的对象
        PreparedStatement pstatement = conn.prepareStatement(sql);
        pstatement.setString(1, adenty.getCid());
        pstatement.setString(2, adenty.getCpwd());
        // 执行sql语句获得结果集
        ResultSet rs = pstatement.executeQuery();
        if (rs.next()) {
            resultAd = new Adenty();
            resultAd.setCid(rs.getString("cid"));
            resultAd.setCpwd(rs.getString("cpwd"));
        }

        return resultAd;
    }

    //添加管理员
    public boolean addAdInfo(Connection conn, Adenty adenty) throws SQLException {
        boolean flag = false;
        String sql = "insert into administrator (cid, cpwd, cname, csex, ccall) values (?,?,?,?,?)";

        PreparedStatement pstm = conn.prepareStatement(sql);
        pstm.setString(1, adenty.getCid());
        pstm.setString(2, adenty.getCpwd());
        pstm.setString(3, adenty.getCname());
        pstm.setString(4, adenty.getCsex());
        pstm.setString(5, adenty.getCcall());

        int res = pstm.executeUpdate();
        System.out.println(res);
        if (res > 0) {
            flag = true;
        }
        return flag;
    }

    //删除管理员
    public boolean deletedemo(Connection conn, Adenty adenty) throws SQLException {//删除操作
        boolean flag = false;
        String sql = "delete from administrator where cid=?and cpwd=?";

        PreparedStatement preparedStatement = conn.prepareStatement(sql);//获取pstmt对象,并传参
        preparedStatement.setString(1, adenty.getCid());
        preparedStatement.setString(2, adenty.getCpwd());


        int res = preparedStatement.executeUpdate();
        System.out.println("flag:" + res);
        if (res == 0) {
            flag = true;
        }
        return flag;
    }

    //    修改管理员
    public boolean updata(Connection conn, Adenty adenty) throws SQLException {
        boolean flag = false;
        String sql = "update administrator set  cpwd=? ,cname=?,csex=?,ccall=?where cid=?";

        PreparedStatement pstm = conn.prepareStatement(sql);
//        pstm.setString(1, adenty.getCid());
        pstm.setString(1, adenty.getCpwd());
        pstm.setString(2, adenty.getCname());
        pstm.setString(3, adenty.getCsex());
        pstm.setString(4, adenty.getCcall());
        pstm.setString(5, adenty.getCid());

        int res = pstm.executeUpdate();
        System.out.println(res);
        if (res > 0) {
            flag = true;
        }
        return flag;
    }

    //    查询管理员
    public Adenty select(Connection conn,Adenty adenty) throws SQLException {
        Adenty resultAd2 = null;
        // sql 查询语句
        String sql = "select * from administrator where cid=?";
//        String sql = "select * from administrator ";
        // 获得执行sql语句的对象
        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setString(1, adenty.getCid());

        // 执行sql语句获得结果集
        ResultSet rs=pstm.executeQuery();
        if (rs.next()) {
            resultAd2 = new Adenty();
            resultAd2.setCid(rs.getString("cid"));
            resultAd2.setCpwd(rs.getString("cpwd"));
            resultAd2.setCname(rs.getString("cname"));
            resultAd2.setCsex(rs.getString("csex"));
            resultAd2.setCcall(rs.getString("ccall"));
        }
        System.out.println("dao层获取的账号:"+resultAd2);
        return resultAd2;
    }

    public List<Adenty> list(Adenty adenty){

        List<Adenty>list=new ArrayList<Adenty>();
        DBConn obj = new DBConn();
        Connection conn = obj.getConn();

        String sql="select * from administrator where cid=?";
//        String sql="select * from administrator ";

        try {
            PreparedStatement pstm= conn.prepareStatement(sql);

            ResultSet resultSet = pstm.executeQuery();

            while (resultSet.next()) {
                adenty=new Adenty();
                adenty.setCid(resultSet.getString("cid"));
                adenty.setCpwd(resultSet.getString("cpwd"));
                adenty.setCname(resultSet.getString("cname"));
                adenty.setCsex(resultSet.getString("csex"));
                adenty.setCcall(resultSet.getString("ccall"));
                list.add(adenty);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
